import 'dart:io';

import 'package:camera/camera.dart';
import 'package:financing_wallet/home/face_recognition/takePictureScreen.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/ht_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/log_util.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/safe_bottom_view.dart';
import 'package:wallet_utils/text_style.dart';
import 'logic.dart';

class FaceRecognitionPage extends StatefulWidget {
  @override
  State<FaceRecognitionPage> createState() => _FaceRecognitionPageState();
}

class _FaceRecognitionPageState extends State<FaceRecognitionPage> {
  final logic = Get.put(FaceRecognitionLogic());

  final state = Get.find<FaceRecognitionLogic>().state;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        logic.backPage();
        return Future.value(true);
      },
      child: Scaffold(
        backgroundColor: ColorManager.colorFFFFFF,
        appBar: htCommonAppbar(context, title: '人脸识别',leadingAction: (){
          logic.backPage();
        }),
        bottomNavigationBar: setBottomButton(
          context,
          '拍摄',
          color: ColorManager.colorFFFFFF,
          hasShadow: false,
          onPressed: () async {
            try {
              // Ensure that the camera is initialized.
              await logic.initializeControllerFuture;
              // Attempt to take a picture and get the file `image`
              // where it was saved.
              var image = await logic.controller.takePicture();
              logic.controller.pausePreview();
              if (!mounted) return;
              LogUtils.d('${image.path}');
              logic.submit(File(image.path));

            } catch (e) {
              // If an error occurs, log the error to the console.
              print(e);
            }
          },
        ),
        body: FutureBuilder<void>(
          future: logic.initializeControllerFuture,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              // If the Future is complete, display the preview.
              return Column(
                children: [
                  Container(
                      margin: EdgeInsets.only(top: 50.w, bottom: 5.w),
                      child: Text(
                        '请确认本人操作',
                        style: TextStyleUtils.style000000(fontSize: 17.sp, fontWeight: FontWeight.bold),
                      )),
                  Text(
                    '请保证正脸在取景框中，并根据屏幕指示完成操作',
                    style: TextStyleUtils.style666666(fontSize: 14.sp),
                  ),
                  Container(
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(top: 30.w,bottom: 30.w),
                    child: ClipOval(
                      child: Container(
                          width: 260.w,
                          height: 260.w,
                          child: CameraPreview(
                            logic.controller,
                          )),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 60.w,right: 60.w),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        buildColumn('images/auth_face_1.png','光线充足'),
                        buildColumn('images/auth_face_2.png','不佩戴眼镜'),
                        buildColumn('images/auth_face_3.png','脸无遮挡'),
                      ],
                    ),
                  )
                ],
              );
            } else {
              // Otherwise, display a loading indicator.
              return const Center(child: CircularProgressIndicator());
            }
          },
        ),
      ),
    );
  }

  Widget buildColumn(String image,String title) {
    return Column(
      children: [
        Image.asset(image, width: 44.w, height: 44.w,),
        SizedBox(height: 5.w,),
        Text(title, style: TextStyleUtils.style666666(fontSize: 12.sp),),
      ],
    );
  }
}
